#!/usr/bin/env bash
# Auto-deploy loop for the online-booking-tool TEST SERVER folder.
# Polls GitHub every 5s; on a new commit, pulls and restarts the mock server.
# This folder is never edited by hand — it only advances via this pull loop.
set -u
cd "$(dirname "$0")"

echo "[watch-and-deploy] watching origin/main, checking every 5s..."

while true; do
  git fetch origin main -q
  LOCAL=$(git rev-parse HEAD)
  REMOTE=$(git rev-parse origin/main)

  if [ "$LOCAL" != "$REMOTE" ]; then
    echo "[watch-and-deploy] new commit detected ($LOCAL -> $REMOTE), deploying..."
    git pull origin main -q
    (cd servicetitan-mock && npm install --silent)
    pm2 restart servicetitan-mock-test
    echo "[watch-and-deploy] deployed $REMOTE. Static files: refresh browser to see changes."
  fi

  sleep 5
done
